<?php
class store_model_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // switch to unit test database from production database
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        
        $this->load->model('store_model');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        $valid_data_test_name = "store->update_store_info : valid data test";
        $invalid_data_test_name = "store->update_store_info : invalid data test";

        // trim function inside store model make input string object, so interger for string field is okay in this test
        // $phone, $name, $address, $city, $state, $zipcode, $footer, $taxrate
        $valid_test = $this->store_model->update_store_info('6589754684', 'great', 'funny', 'new york', 'NY', '11220-3243', 'great', '9.43');
        $invalid_test1 =  $this->store_model->update_store_info('12', 23, 32, 23, 43, 234, 23, 'aa');
        $invalid_test2 =  $this->store_model->update_store_info('9856asdf', 23, 32, 23, 43, '23asdf4sw3svasdfwef23', 23, '500');
        $update_test = $this->store_model->get_store_info();

        $this->unit->run(count($valid_test['user']), 0, $valid_data_test_name);
        $this->unit->run(count($invalid_test1['user']), 2, $invalid_data_test_name);
        $this->unit->run(count($invalid_test2['user']), 3, $invalid_data_test_name);
        if("great" == $update_test['name'] && "9.43" == $update_test['taxrate'])
        {
            $this->unit->run(1, 1, "update confirm test");
        }

        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }
}